<template>
  <div>
    <c-table
      :loading="loading"
      :height="height"
      :table-column="experimentColumn"
      :table-data="list"
      is-selection
      :is-delete="false"
      :operation="!disabled"
      @on-save="onSave"
      @modify="modifyHandle"
      @focus="focusHandle"
    />
    <issue-tracking-dialog
      v-if="issueTrackingData.show"
      :params="issueTrackingData.params"
      @close="issueTrackingCloseHandle"
    />
  </div>
</template>

<script>
import IssueTrackingDialog from "@/components/IssueTrackingDialog.vue";
import { overviewApis } from "@/apis";
import { experimentColumn } from "../data";

export default {
  components: {IssueTrackingDialog},
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    list: {
      type: Array,
      default() {
        return []
      }
    },
    height: {
      type: String,
      default: null
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      experimentColumn,
      type: {
        kitting: "ZXTQT",
        open: "ZXTZK",
        continuity: "ZXTLXXCS",
        facility: "ZXTGNCS",
      },
       issueTrackingData: {
        show: false,
        params: {
          billId: 0,
          type: "",
        },
      },
    }
  },
  methods: {
    async onSave(row) {
      await overviewApis.progressExperimentSave(row)
      this.refresh()
    },
    modifyHandle(key, row, isComplete) {
      if (this.disabled) {
        return
      }
      this.cacheModifyData = {
        key,
        row
      }
      /**
       * ZXTQT("ZXTQT", "子系统齐套"),
          ZXTZK("ZXTZK", "子系统展开"),
          ZXTLXXCS("ZXTLXXCS", "子系统连续性测试"),
          ZXTGNCS("ZXTGNCS", "子系统功能测试"),
       */
      if (isComplete) {
        overviewApis.saveProgressRecord({
          billId: row.id,
          type: this.type[key],
          complete: 1
        }).then(() => {
          //  this.refresh()
          this.issueTrackingRefreshRow()
        })
      } else {
        this.issueTrackingData.show = true;
        this.issueTrackingData.params.billId = row.id;
        this.issueTrackingData.params.type = this.type[key];
      }
      
    },
    issueTrackingCloseHandle(refresh) {
      if (refresh) {
        // this.refresh()
        this.issueTrackingRefreshRow()
      }
      this.issueTrackingData.show = false;
    },
    async focusHandle(row) {
      if (this.disabled) {
        return
      }
      await overviewApis.progressExperimentReadyFocus({ id: row.id });
      this.refresh()
    },
    refresh() {
      this.$emit('refresh')
    },
    issueTrackingRefreshRow() {
      const {key, row} = this.cacheModifyData
      const valueMap = this.experimentColumn[key].valueMap
      overviewApis.progressExperimentFindPageList({
          id: row.id
        })
        .then(({ data }) => {
          const { list } = data
          const newData = list[0]
          Object.assign(row, {
            [valueMap.value]: newData[valueMap.value],
            [valueMap.progress]: newData[valueMap.progress],
            [valueMap.count]: newData[valueMap.count],
            expColor: newData['expColor'],
            expProp: newData['expProp'],
          })
        })
    }
  }
}
</script>

<style>

</style>